//
//  TYBtnView.m
//  爱鲜蜂
//
//  Created by 李田 on 16/11/18.
//  Copyright © 2016年 tianyao. All rights reserved.
//

#import "LBBtnView.h"
@implementation LBBtnView

//- (instancetype)initWithFrame:(CGRect)frame{
//    
//    if (self = [super initWithFrame:frame]) {
//        
//        [self setupUI];
//    }
//    return self;
//}

- (instancetype)initWithImageNameArr: (NSArray *)imageNameArr titleArr: (NSArray *)titleArr{
    LBBtnView *btnV = [[LBBtnView alloc] init];
    
    [btnV createButtonWithImageName:imageNameArr[0] title:titleArr[0] tag:kFirstButton];
    [btnV createButtonWithImageName:imageNameArr[1] title:titleArr[1] tag:kSencondButton];
    

    [btnV createButtonWithImageName:imageNameArr[2] title:titleArr[2] tag:kThirdButton];

    [btnV createButtonWithImageName:imageNameArr[3] title:titleArr[3] tag:kFourthButton];
    
    return btnV;
}

//- (void)setupUI{
//    
//    [self createButtonWithImageName:@"member_icon_0" title:@"每日签到" tag:kFirstButton];
//    [self createButtonWithImageName:@"member_icon_1" title:@"疯狂秒杀" tag:kSencondButton];
//    [self createButtonWithImageName:@"member_icon_2" title:@"鲜货直供" tag:kThirdButton];
//    [self createButtonWithImageName:@"member_icon_3" title:@"大闸蟹" tag:kFourthButton];
//    
//}

#pragma mark -自定义button
- (void)createButtonWithImageName: (NSString *)imageName title: (NSString *)title tag: (TYTopBtnTag)tag{
    //    MARK: 创建一个按钮
    UIButton *btn = [[UIButton alloc] init];
    //    MARK: 添加到cell中
    [self addSubview:btn];
    //    MARK: 生成一个图片和文字混合的NSAttributedString（图文混排）
//    NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:imageName] imageWH:44 title:title fontSize:12 titleColor:[UIColor blackColor] spacing:7];
    NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:imageName] imageW:69 imageH:45 title:title fontSize:12 titleColor:[UIColor blackColor] spacing:7];
    //    MARK: 设置按钮的nsattributed属性
    [btn setAttributedTitle:str forState:UIControlStateNormal];
    //    MARK: 设置按钮的大小
    [btn sizeToFit];
    //    MARK: 设置按钮的自动换行
    btn.titleLabel.numberOfLines = 0;
    //    MARK: 设置按钮的文本对齐方式
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    //    MARK: 给按钮添加标签
    btn.tag = tag;
    //    MARK: 设置按钮的点击事件
    [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - btn 的点击事件
- (void)btnClickAction: (UIButton *)btn{
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"button" object:btn userInfo:nil];
    
}
#pragma mark - 布局
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    //    MARK: 计算按钮之间的间距
    CGFloat margin = (self.frame.size.width - self.subviews.count *self.subviews[0].frame.size.width) / (self.subviews.count + 1);
    
    for(NSInteger i = 0; i < self.subviews.count - 1; i++){
        UIButton *btn = self.subviews[i];
        UIButton *nextBtn = self.subviews[i + 1];
        
        if (i == 0) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.left.mas_equalTo(margin);
            }];
        }
        
        [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(btn.mas_right).offset(margin);
        }];
    }

    
    
}



@end
